package net.cohoivang.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.cohoivang.core.Company;
import net.cohoivang.core.Industry;
import net.cohoivang.core.ProfileCompany;
import net.cohoivang.core.Sector;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

@SuppressWarnings("serial")
public class ControllerServlet extends HttpServlet {
	
	protected static final Gson gson = new Gson();
	public ControllerServlet() {
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
	
		System.out.println("Got request: " + req.getQueryString() + " from " + req.getRemoteAddr() +
				" (" + req.getRemoteHost() + ").");
		
		String action = req.getParameter("action");
		
		String sessionID = (String) req.getSession().getAttribute("golden_session_key");
		resp.setContentType("text/html");
		resp.setCharacterEncoding("UTF-8");
		PrintWriter outputSteam = resp.getWriter();
		
		if ("myAjaxAction".equals(action)) {
			String myParameter = req.getParameter("abc");
			System.out.println("we receive param :" + myParameter);
			//resp.setContentType("text/html");
			//resp.setCharacterEncoding("UTF-8");
			outputSteam.write("hello world");
		}
		if("getListIndustry".equals(action)){
			//String myParameter = req.getParameter("abc");
			
			//resp.getWriter().write("hello world");
			String sectorId = req.getParameter("sectorId");
			int sec = Integer.parseInt(sectorId);
			
			Sector sector = ProfileCompany.getInstance().findSector(sec);
			List<Industry> listIndustry = sector.getIndustrys();
			JsonObject result = new JsonObject();
			JsonArray arr = new JsonArray();
			for(int i=0;i<listIndustry.size();i++){
				 JsonObject temp = new JsonObject();
				 temp.addProperty("industryId", listIndustry.get(i).getIndustryId());
				 temp.addProperty("industryName", listIndustry.get(i).getIndustryName());
				 arr.add(temp);
			}
			result.add("listIndustry", arr);
			outputSteam.println(gson.toJson(result));
			outputSteam.flush();
			System.out.println("get list indutry"+listIndustry.size()+"\n");
		}
		if("getListCompany".equals(action)){
			String industryId = req.getParameter("industryId");
			int in = Integer.parseInt(industryId);
			
			Industry industry = ProfileCompany.getInstance().findIndustry(in);
			List<Company> listCompany = industry.getAllCompany();
			JsonObject result = new JsonObject();
			JsonArray arr = new JsonArray();
			for(int i=0;i<listCompany.size();i++){
				 JsonObject temp = new JsonObject();
				 temp.addProperty("companyId", listCompany.get(i).getCompanyId());
				 temp.addProperty("companyName", listCompany.get(i).getLegalName());
				 arr.add(temp);
			}
			result.add("listCompany", arr);
			outputSteam.println(gson.toJson(result));
			outputSteam.flush();
			System.out.println("get list company"+listCompany.size()+"\n");
			 
		}
		
	}

}
